package com.fruits.login.controller;

import com.fruits.login.annotation.DeCrypt;
import com.fruits.login.dto.LoginDto;
import com.fruits.login.model.*;
import com.fruits.login.model.properties.LoginProperties;
import com.fruits.login.service.CheckService;
import com.fruits.login.service.CodeService;
import com.fruits.login.service.LoginService;
import com.fruits.util.IPUtil;
import com.fruits.util.ParameterDetectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private CodeService codeService;

    @Autowired
    private CheckService checkService;

    /**
     * 获取验证码
     *
     * @param getPhoneCode
     * @return
     */
    @PostMapping("/getCode")
    public ReturnMsg getCode(@DeCrypt GetPhoneCode getPhoneCode) {
        log.info(getPhoneCode.getPhone() + "获取验证码");
        ReturnMsg phoneMsg = ParameterDetectionUtil.checkPhone(getPhoneCode.getPhone());
        if (phoneMsg.getState() == 0) {
            return phoneMsg;
        }
        ReturnMsg msg = new ReturnMsg();
        String code = codeService.createAndSaveCode(getPhoneCode.getPhone());
        if (code == null) {
            msg.setState(0);
            msg.setInfo("请勿重复频繁获取验证码");
            return msg;
        }
        log.info("验证码:" + code);
        if (code != null && code.length() == LoginProperties.SMS_CODE_SIZE) {
            boolean flag = codeService.sendCode(getPhoneCode.getPhone(), code);
            //TODO 判断发送成功还是失败
            if (flag) {
                msg.setState(1);
                msg.setInfo("短信验证码已发送,请注意查收");
            } else {
                msg.setInfo("短信验证码发送失败,请重新获取");
                msg.setState(0);
            }
        } else {
            msg.setState(0);
            msg.setInfo("获取验证码失败,请重新获取");
        }
        return msg;
    }

    /**
     * 登录
     *
     * @param loginDto
     * @return
     * @RequestBody(required=false)
     */
    @PostMapping("/login")
    public ReturnMsg playerLogin(@DeCrypt LoginDto loginDto, HttpServletRequest request) {
        log.info("登录:" + loginDto.toString());
        String ip = IPUtil.getIPAddress(request);
        log.info("IP" + ip);

        PlayerLoginInfo playerLoginInfo = PlayerLoginInfo.copy(loginDto);
        boolean isSign = loginService.signValidator(playerLoginInfo);

        if (playerLoginInfo.getPhone() != null) {
            log.info(playerLoginInfo.getPhone() + "手机号登录");
            return loginService.loginByPhoneAndCode(playerLoginInfo);
        } else if (playerLoginInfo.getUid() != null) {
            log.info(playerLoginInfo.getUid() + "uid登录");
            return loginService.loginByUid(playerLoginInfo);
        } else if (playerLoginInfo.getDeviceMac() != null) {
            log.info(playerLoginInfo.getDeviceMac() + "deviceMac登录");
            return loginService.loginByMac(playerLoginInfo);
        }
        return loginService.retuenPlayerInfoWhereFaile("缺少参数");
    }


    /**
     * 版本检测
     *
     * @return
     */
    @RequestMapping(value = "/version", method = RequestMethod.POST)
    public ReturnMsg versionCheck(@DeCrypt ConfigVersionInfo version) {
        log.info("版本检测" + version.getVersion() + ":" + version.getSysId());
        return checkService.checkVersion(version);
    }

    /**
     * 资源检测
     *
     * @return
     */
    @PostMapping("/resource")
    public ReturnMsg resourceCheck(@DeCrypt ConfigResourceInfo configResourceInfo) {
        log.info("资源检测" +  configResourceInfo.getVersion() + ":" + configResourceInfo.getSysId());
        return checkService.checkResource(configResourceInfo);
    }
}
